package com.example.xgakj.newterminal.ui.framents;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.xgakj.mylibrary.mvp.utils.FragmentHelper;
import com.example.xgakj.newterminal.R;
import com.example.xgakj.newterminal.adapter.DeviceDataAdapter;
import com.example.xgakj.newterminal.base.BaseApplication;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.beans.CustomerBean;
import com.example.xgakj.newterminal.models.BaseJsonCallBack;
import com.example.xgakj.newterminal.models.QueryModel;
import com.example.xgakj.newterminal.ui.activities.DeviceManageActivity;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.model.Response;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class DeviceListFragment extends Fragment {


    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    Unbinder unbinder;
    private DeviceDataAdapter mDataAdapter;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private boolean end;
    private String field;
    private String param_filter;
    private int mCurrentCounter=1;
    private FragmentHelper helper;

    public DeviceListFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_device_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        initArguments();
        initRecycler();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        helper = ((DeviceManageActivity) context).getHelper();
    }

    private void initArguments() {

        field="CUID,Status,RecvTime,Name,PartID,Address,PhoneHome,TC_Type,partname,MemborMobel1,MemborMobel2,MemborName1,MemborName2";
        String key= BaseApplication.classKey;
        String value=BaseApplication.classValue;
        if(3==BaseApplication.flag)
        {
            param_filter="1='1'";
        }
        else
        {
            param_filter =key+"='"+value+"'";
        }
    }

    private void initRecycler() {
        mDataAdapter = new DeviceDataAdapter(getActivity());
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        recyclerView.setAdapter(mLRecyclerViewAdapter);

        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.default_divider_height)
                .setPadding(R.dimen.default_divider_padding)
                .setColorResource(R.color.split)
                .build();

        //recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mDataAdapter.clear();
                mLRecyclerViewAdapter.notifyDataSetChanged();//fix bug:crapped or attached views may not be recycled. isScrap:false isAttached:true
                mCurrentCounter = 1;
                end = false;
                getDeviceList();


            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (!end) {
                    getDeviceList();
                } else {
                    //the end
                    recyclerView.setNoMore(true);

                }
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle = new Bundle();

                bundle.putSerializable(Parameters.CUSTOMER_BEAN,mDataAdapter.getDataList().get(position));
                DeviceDetailFragment detailFragment = new DeviceDetailFragment();
                detailFragment.setArguments(bundle);
                helper.addFragmentToBackStack(detailFragment);

            }
        });
        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.dark, android.R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        //第一次进入调用刷新数据
        recyclerView.refresh();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    /**
     * 查询报警记录列表
     */
    public void getDeviceList() {
        QueryModel.getInstance().getListByPage(Parameters.T_VIEW_CUSTMEMOBERS, field,param_filter, "15", String.valueOf(mCurrentCounter), "CUID", "asc",
                new BaseJsonCallBack<List<CustomerBean>>(null) {
                    @Override
                    public void onError(Response<List<CustomerBean>> response) {
                        super.onError(response);
                        recyclerView.refreshComplete(0);
                    }

                    @Override
                    public void onSuccess(Response<List<CustomerBean>> response) {

                        List<CustomerBean> beanList = response.body();
                        if (beanList!=null&&beanList.size()>0){
                            mDataAdapter.addAll(beanList);
                            recyclerView.refreshComplete(beanList.size());
                            if (beanList.size()<15){
                                recyclerView.setNoMore(true);
                            }
                        }
                    }
                }
                );
        mCurrentCounter++;
    }

}
